﻿Imports System.Windows.Forms
Imports QueueManager.Service.Config
Imports System.Configuration

Public Class Settings

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        'Me.DialogResult = System.Windows.Forms.DialogResult.OK
        ApplySettings()
        'Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Settings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim objSettings As QueueSection

        With ConfigurationManager.AppSettings
            txtServer.Text = .Item("crmserviceurl")
            txtOrg.Text = .Item("organization")
            txtUsername.Text = .Item("username")
            txtDomain.Text = .Item("domain")
            txtPassword.Text = .Item("password")
            Boolean.TryParse(.Item("logeventlog"), chkEventLog.Checked)
            txtErrorRcp.Text = .Item("logerrornotify")
            txtWarningRcp.Text = .Item("logwarningnotify")
            txtEventSender.Text = .Item("lognotifyfrom")
            txtLogFile.Text = .Item("logtofile")
        End With

        objSettings = CType(ConfigurationManager.GetSection("queues"), QueueSection)

        cboQueues.DisplayMember = "Name"
        cboQueues.ValueMember = "Name"

        For Each objItem As QueueSetting In objSettings.Queues
            cboQueues.Items.Add(objItem)
        Next

    End Sub

    Private Sub ApplySettings()

        Dim objConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        With objConfig.AppSettings
            .Settings("crmserviceurl").Value = txtServer.Text
            .Settings("organization").Value = txtOrg.Text
            .Settings("username").Value = txtUsername.Text
            .Settings("domain").Value = txtDomain.Text
            .Settings("password").Value = txtPassword.Text
            .Settings("logeventlog").Value = chkEventLog.Checked.ToString.ToLower
            .Settings("logerrornotify").Value = txtErrorRcp.Text
            .Settings("logwarningnotify").Value = txtWarningRcp.Text
            .Settings("lognotifyfrom").Value = txtEventSender.Text
            .Settings("logtofile").Value = txtLogFile.Text
        End With

        objConfig.Save(ConfigurationSaveMode.Modified, False)

    End Sub

End Class
